/* 角色管理 */
import React, { PureComponent } from 'react';
import { Table, Button, Input, Form, Modal, Checkbox, message } from 'antd';
import { addAuthority, getAuthority, editAuthority, deleteAuthority } from '@/services/peoples'
import { hasNotice, isAnte, hasApply, isHangda, isZhengda ,hasIsSw,isLianxiSite,isZhongZheng} from '@/utils/utils'
import './index.less'
import { isHengTai } from '../../../utils/utils';
const CheckboxGroup = Checkbox.Group;
const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 17 },
}

class Index extends PureComponent<any, any> {
  constructor(props: any) {
    super(props);
    this.state = {
      dataSource: [],
      addVisible: false,
      searchName: '',
      name: '',
      power: [],
      count: 1,
      editVisible: false,
      editName: '',
      editPower: [],
      id: '',
      loading: false
    }
  }
  renderList = () => {
    const { searchName } = this.state;
    getAuthority({ name: searchName }).then(res => {
      if (!res || !res[0]) return;
      this.setState({
        dataSource: res,
        count: res.length
      })
    })
  }
  showAdd = () => {
    this.setState({
      addVisible: true
    })
  }
  hideAdd = () => {
    this.setState({
      addVisible: false
    })
  }
  changeName = (e: any) => {
    this.setState({
      name: e.target.value
    })
  }
  changeAuthority = (e: any) => {
    this.setState({
      power: e
    })
  }
  submitHandle = () => {
    const { name, power } = this.state;
    this.setState({
      loading: true
    })
    addAuthority({ name, power }).then(res => {
      this.setState({
        loading: false
      })
      if (res.status === 1) {
        message.success('添加成功')
        this.setState({
          addVisible: false
        })
        this.renderList()
      } else {
        message.error('添加失败')
      }
    })
  }
  changeSearchName = (e: any) => {
    this.setState({
      searchName: e.target.value
    })
  }
  searchHandle = () => {
    this.renderList()
  }

  showEdit = (row: any) => () => {
    this.setState({
      editVisible: true,
      editName: row.name,
      editPower: row.power && row.power.split(',').map(Number),
      id: row.id
    })
  }
  hideEdit = () => {
    this.setState({
      editVisible: false
    })
  }
  changeEditName = (e: any) => {
    this.setState({
      editName: e.target.value
    })
  }
  changeEditAuthority = (e: any) => {
    this.setState({
      editPower: e
    })
  }
  submitEdit = () => {
    const { editName, editPower, id } = this.state;
    this.setState({
      loading: true
    })
    editAuthority({ name: editName, power: editPower, id }).then(res => {
      this.setState({
        loading: false
      })
      if (res.status === 1) {
        message.success('编辑成功')
        this.setState({
          editVisible: false
        })
        this.renderList()
      } else {
        message.error('编辑失败')
      }
    })
  }
  deleteHandle = (id: any) => () => {
    Modal.confirm({
      title: '提示',
      content: '确定删除该角色？',
      okText: '确定',
      cancelText: '取消',
      onOk: () => {
        deleteAuthority({ id }).then(res => {
          if (res.status === 1) {
            message.success('已删除')
            this.renderList();
          } else {
            message.error('删除失败')
          }
        })
      }
    })
  }
  componentDidMount() {
    this.renderList()
  }
  componentWillUnmount = () => {
    this.setState = (state, callback) => {
      return;
    };
  }

  render() {
    const userinfo = JSON.parse(localStorage.getItem('userinfo') || '{}');
    const checkboxList: any = [
      {
        id: 1,
        name: isLianxiSite()?'用户管理权限':'用户管理权限（特种设备）',
        data: [
          { value: 101, label: '用户列表' },
          { value: 102, label: '用户增加' },
          { value: 103, label: '用户修改' },
          { value: 119, label: '审核用户列表' },
          { value: 120, label: '审核用户' },
          { value: 104, label: '用户禁用' },
          { value: 105, label: '用户删除' },
          { value: 106, label: '单位结构' },
          { value: 107, label: '职务岗位' },
          // { value: 108, label: '审核用户' },
          { value: 109, label: '角色管理' },
          { value: 110, label: '无部门用户管理' },
          { value: 111, label: '积分管理' },

        ]
      },
      hasIsSw()?{
        id: 9,
        name: '用户管理权限（特种作业）',
        data: [
          { value: 901, label: '用户列表' },
          { value: 902, label: '用户增加' },
          { value: 903, label: '用户修改' },
          { value: 919, label: '审核用户列表' },
          { value: 920, label: '审核用户' },
          { value: 904, label: '用户禁用' },
          { value: 905, label: '用户删除' },
          { value: 906, label: '单位结构' },
          { value: 907, label: '职务岗位' },
          // { value: 908, label: '审核用户' },
          { value: 909, label: '角色管理' },
          { value: 910, label: '无部门用户管理' },
          { value: 911, label: '积分管理' },

        ]
      }:{},
      {
        id: 2,
        name: isLianxiSite()?'考试科目权限':'考试科目权限（特种设备）',
        data: [
          { value: 201, label: '科目分类' },
          { value: 202, label: '增加科目' },
          { value: 203, label: '修改科目' },
          { value: 204, label: '删除科目' },
        ]
      },
      hasIsSw()?{
        id: 9999,
        name:'考试科目权限（特种作业）',
        data: [
          { value: 99991, label: '科目分类' },
          { value: 99992, label: '增加科目' },
          { value: 99993, label: '修改科目' },
          { value: 99994, label: '删除科目' },
        ]
      }:{},
      userinfo.super === 1 ? {
        id: 3,
        name:isLianxiSite()?'考试题库权限':'考试题库权限（特种设备）',
        data: [
          { value: 301, label: '题库列表' },
          { value: 302, label: '增加试题' },
          { value: 303, label: '修改列表' },
          { value: 304, label: '启用禁用' },
          { value: 305, label: '删除试题' },
          { value: 306, label: '批量管理' },
          { value: 307, label: '统计分析' },
          { value: 308, label: '禁用题库' },
        ]
      } : {},
      userinfo.super === 1&&hasIsSw() ? {
        id: 99995,
        name:'考试题库权限（特种作业）',
        data: [
          { value: 99996, label: '题库列表' },
          { value: 99997, label: '增加试题' },
          { value: 99998, label: '修改列表' },
          { value: 99999, label: '启用禁用' },
          { value: 999990, label: '删除试题' },
          { value: 999991, label: '批量管理' },
          { value: 999992, label: '统计分析' },
          { value: 999993, label: '禁用题库' },
        ]
      } : {},
    
      {
        id: 5,
        name:isLianxiSite()?'试卷信息权限':'试卷信息权限（特种设备）',
        data: [
          { value: 501, label: '随机组卷' },
          { value: 502, label: '人工组卷' },
          { value: 503, label: '修改试卷' },
          { value: 504, label: '删除试卷' },
          { value: 505, label: '添加模板' },
          { value: 506, label: '修改模板' },
        ]
      },
     hasIsSw()? {
        id: 9999999,
        name: '试卷信息权限（特种作业）',
        data: [
          { value: 99999991, label: '随机组卷' },
          { value: 99999992, label: '人工组卷' },
          { value: 99999993, label: '修改试卷' },
          { value: 99999994, label: '删除试卷' },
          { value: 99999995, label: '添加模板' },
          { value: 99999996, label: '修改模板' },
        ]
      }:{},
      {
        id: 4,
        name: '电子教材权限',
        data: [
          { value: 401, label: '添加教材' },
          { value: 402, label: '修改教材' },
          { value: 403, label: '删除教材' },
        ]
      },
      {
        id: 6,
        name: '统计管理权限',
        data: [
          { value: 601, label: '练习统计删除' },
          { value: 602, label: '模拟统计删除' },
          { value: 603, label: '学习统计删除' },

        ]
      },
      {
        id: 7,
        name: '系统管理权限',
        data: [
          { value: 701, label: '系统设置' },
          { value: 702, label: '系统日志' },
        ]
      },

    ]
    const columns = [
      {
        title: '角色名称',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '操作',
        dataIndex: 'id',
        key: 'id',
        render: (id: any, row: any) => {
          return (
            <div>
              <a style={{ marginRight: 30 }} onClick={this.showEdit(row)}>编辑</a>
              <a onClick={this.deleteHandle(id)}>删除</a>
            </div>
          );
        },
      },
    ];
    const { dataSource, addVisible, editVisible, editName, editPower, loading } = this.state;
    if (hasNotice()) {//有的平台开公告管理
      checkboxList.push({
        id: 8,
        name: '公告管理权限',
        data: [
          { value: 801, label: '培训动态' },
          { value: 802, label: '学员公告' },
          { value: 803, label: '政策法规' },
        ]
      })
    }
    if (isAnte()) {//用户管理权限
      checkboxList[0].data.push({ value: 112, label: '审核用户添加' });
      checkboxList[0].data.push({ value: 113, label: '审核操作' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 912, label: '审核用户添加' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 913, label: '审核操作' });
    }
    if (hasApply()) {//系统管理权限
      checkboxList[10].data.push({ value: 703, label: '申请表管理' });
    }
    if (isHangda()) {//公告管理权限
      checkboxList[11].data.push({ value: 804, label: '业务介绍' });
      checkboxList[11].data.push({ value: 805, label: '考试计划' });
      checkboxList[11].data.push({ value: 806, label: '考生必读(应急)' });
      checkboxList[11].data.push({ value: 807, label: '政策法规(市场)' });
      checkboxList[11].data.push({ value: 808, label: '轮播图设置' });
    }
    if (isZhengda()) {//用户管理权限
      checkboxList[0].data.push({ value: 114, label: '批量上传时间' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 914, label: '批量上传时间' });
    }
    if(isZhongZheng()){
      checkboxList[0].data.push({ value: 115, label: '缴费用户添加' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 915, label: '缴费用户添加' });
      checkboxList[0].data.push({ value: 116, label: '缴费财务审核' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 916, label: '缴费财务审核' });
      checkboxList[0].data.push({ value: 117, label: '缴费主管审核' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 917, label: '缴费主管审核' });
    }
    if(isZhongZheng()||isHengTai()){
      checkboxList[0].data.push({ value: 118, label: '搜题权限' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 918, label: '搜题权限' });
      checkboxList[0].data.push({ value: 121, label: '修改备注权限' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 921, label: '修改备注权限' });
      checkboxList[0].data.push({ value: 122, label: '查看备注权限' });
      checkboxList[1].data&&checkboxList[1].data.push({ value: 922, label: '查看备注权限' });
    }
    return (
      <div>
        <div style={{ overflow: 'hidden' }}>
          <Form layout="inline" style={{ marginBottom: 20, float: 'right' }}>
            <Form.Item>
              <Input placeholder="搜索角色名称" onChange={this.changeSearchName} />
            </Form.Item>
            <Form.Item>
              <Button type="primary" onClick={this.searchHandle}>查询</Button>
            </Form.Item>
            <Form.Item>
              <Button type="primary" danger onClick={this.showAdd}>
                增加
              </Button>
            </Form.Item>
          </Form>
        </div>
        <Table rowKey="id" pagination={{ pageSize: 10 }} columns={columns} dataSource={dataSource}></Table>
        <Modal confirmLoading={loading} visible={addVisible} title="增加角色" onCancel={this.hideAdd} onOk={this.submitHandle}>
          <Form>
            <Form.Item {...layout} label="角色名称">
              <Input placeholder="请输入角色名称" onChange={this.changeName}></Input>
            </Form.Item>
            <CheckboxGroup onChange={this.changeAuthority}>
              {
                checkboxList.map((item: any) => {
                  return item.id && <Form.Item {...layout} label={item.name} key={item.id}>
                    {
                      item.data && item.data.map((item: any) => {
                        return <Checkbox key={item.value} value={item.value}>{item.label}</Checkbox>
                      })
                    }
                  </Form.Item>
                })
              }
            </CheckboxGroup>
          </Form>
        </Modal>
        <Modal confirmLoading={loading} visible={editVisible} title="修改角色" onCancel={this.hideEdit} onOk={this.submitEdit}>
          <Form>
            <Form.Item {...layout} label="角色名称">
              <Input placeholder="请输入角色名称" onChange={this.changeEditName} value={editName}></Input>
            </Form.Item>
            <CheckboxGroup onChange={this.changeEditAuthority} value={editPower}>
              {
                checkboxList.map((item: any) => {
                  return item.id && <Form.Item {...layout} label={item.name} key={item.id}>
                    {
                      item.data && item.data.map((item: any) => {
                        return <Checkbox key={item.value} value={item.value}>{item.label}</Checkbox>
                      })
                    }
                  </Form.Item>
                })
              }
            </CheckboxGroup>
          </Form>
        </Modal>
      </div>
    );
  }
}

export default Index;
